package edu.uta.BillMeApp;

import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import edu.BillME.datapars.HttpConnection;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/* @auther Siddharth Patel & Susan Foster */
public class RecentBills extends ListActivity{
	
	JSONArray jArray;
	String result = null;
	StringBuilder sb=null;
	InputStream is = null;
	final static String URL="http://omega.uta.edu/~smp3753/recentbill.php";
	
	String obj1;
	String obj2;
	String obj3;
	String obj4;
	String obj5;
	String obj6;
	String obj7;
	String x,y;
	String amount;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
	   super.onCreate(savedInstanceState);
	   Bundle gotBundle=getIntent().getExtras();
       final String getUN = gotBundle.getString("wN");
       Log.d("getUN",getUN);
       
       ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
       postParameters.add(new BasicNameValuePair("username", getUN));
	   ArrayAdapter<String> servers = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
	
	   //paring data
	   try{
			  
	       jArray = new JSONArray(HttpConnection.executeHttpPost(URL,postParameters));
	       
	       for(Integer i=0; i< jArray.length(); i++){
	           try{
	        	   Log.d("jarray",jArray.toString());
	               //Get My JSONObject and grab the String Value that I want.
                    obj1 = jArray.getJSONObject(i).getString("FriendsName");
                    obj2 = jArray.getJSONObject(i).getString("UserName");
                    obj3 = jArray.getJSONObject(i).getString("Transaction_ID");
                    obj4 = jArray.getJSONObject(i).getString("description");
                    obj5 = jArray.getJSONObject(i).getString("date");
                    obj6 = jArray.getJSONObject(i).getString("Confirmation");
                    obj7 = jArray.getJSONObject(i).getString("monthly_flag");
                    
                   String status;
                   if(obj6.contains("1")){
                	   status="Comfirmed";
                	   
                   }else{
                	   status="Pending";

                	   String res=obj2.toString();
                	   res= res.replace("\n","");
                	   Log.d("recent bill obj2",obj2);
                	   amount = jArray.getJSONObject(i).getString("AmountToBeReturned");
                	   if(obj1.contains("getUN")){
                    	   Log.e("value at friends history in side",obj2);
                    	   x=obj2;
                    	   y=obj1;
                    	   String obj = (""	+x+" posted $ "+amount+" to "+y+" \nBill-ID: "+obj3+" \nstatus:"+status+" \nDescription:"+obj4);
                    	   servers.add(obj);
                       }else{
                    	   y=obj2;
                    	   x=obj1;
                    	   String obj = (""	+y+" posted $ "+amount+" to "+x+" \nBill-ID "+obj3+" \nstatus:"+status+" \nDescription:"+obj4);
                    	   servers.add(obj);
                       }
                   }
                   
	               setListAdapter(servers);
	               
	           }catch(JSONException e){
	            Toast.makeText(getBaseContext(), "Not Found" ,Toast.LENGTH_LONG).show();
	           }	 
	       }

	   }catch (Exception e1) {
	    Log.e("friend trans", "Error final1212 "+e1.toString());
	    }

	   ListView lv = getListView();
	   lv.setTextFilterEnabled(true);
	      
	   lv.setOnItemClickListener(new OnItemClickListener(){

		   public void onItemClick(AdapterView<?> arg0, View arg1, int arg3, long arg4){
			   String friendName=((TextView) arg1).getText().toString();
			   
				Bundle bundle = new Bundle();
				bundle.putString("fN",friendName);
				bundle.putString("wN",getUN);
				
			   Intent myIntent = new Intent(arg1.getContext(), Confirmation.class);
			   myIntent.putExtras(bundle);
	           startActivityForResult(myIntent, 0);
	          
		   }	  
	   });
	}
}